package Messages;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import ocsf.server.ConnectionToClient;
import server.SQLConnection;
/**
 * This class implements search phone in the DB
 * @author Gabi Revah
 * @author Aviad Almog
 */ 
public class MessageSearchPhone {
	
	private ConnectionToClient client ;
	private String[] str; 

/**
 * Contractor of this class 
 * @param str
 * @param client
 */
	public MessageSearchPhone(String[] str, ConnectionToClient client){
		
		this.client= client ;
		this.str= str; 	
	
	}
	
	/**
	 * This method search phone in the DB 
	 * @throws SQLException
	 * @throws IOException
	 */
	public void SearchPhone() throws SQLException, IOException{
	
				SQLConnection sc = new SQLConnection();
				java.sql.PreparedStatement bid = sc.getConnection().prepareStatement("SELECT * FROM db.private Where phone ='"+str[1]+"';");
				ResultSet bidrs= bid.executeQuery();
				
				java.sql.PreparedStatement bid1 = sc.getConnection().prepareStatement("SELECT * FROM db.business Where phone ='"+str[1]+"';");
				ResultSet bidrs1= bid1.executeQuery();
				
				 if(bidrs.next())
				{				
										
					
										ArrayList<Object> bus=new ArrayList<Object>();
										bus.add("SearchPrivate");
										bus.add(bidrs.getInt(1));
										bus.add(bidrs.getString(2));
										bus.add(bidrs.getString(3));
										bus.add(bidrs.getString(4));
										bus.add(bidrs.getString(5));
										bus.add(bidrs.getString(6));
										
										
										client.sendToClient(bus);
									
								
				}
	
				 else if(bidrs1.next())
				{				
										
					
										ArrayList<Object> bus1=new ArrayList<Object>();
										bus1.add("SearchBussines");
										bus1.add(bidrs1.getString(1));
										bus1.add(bidrs1.getString(2));
										bus1.add(bidrs1.getString(3));
										bus1.add(bidrs1.getString(4));
										bus1.add(bidrs1.getString(5));
										bus1.add(bidrs1.getString(6));									
										
										client.sendToClient(bus1);
									
								
								}	
				 
				  else if (!bidrs.next() && !bidrs1.next())
				{
					client.sendToClient("PhoneNotFound");

				}
				
				
}
}

				
				        	
			


